package com.example.softwareconfigurationcoursedesign.Admin;

import com.example.softwareconfigurationcoursedesign.LoginRegister.User;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {

    // 处理POST请求
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");

        // 获取前端传递的参数
        String name = request.getParameter("userName"); // 获取用户姓名
        String phoneNumber = request.getParameter("phoneNumber"); // 获取用户手机号码
        String password = request.getParameter("password"); // 获取用户密码

        // 输出用户信息到控制台
        System.out.println("添加用户信息：");
        System.out.println("姓名：" + name);
        System.out.println("手机号码：" + phoneNumber);
        System.out.println("密码：" + password);

        // 在这里执行添加会员的逻辑
        AdminServletImplement AdminServletImplement = new AdminServlet(); // 实例化AdminServletImplement对象
        User user = new User(0, phoneNumber, name, password); // 创建用户对象
        AdminServletImplement.addUserToDatabase(user); // 调用添加用户到数据库的方法
        System.out.println("添加会员");

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<script>setTimeout(function() { window.close(); }, 1);</script>"); // 返回JavaScript脚本，关闭当前页面
        out.close();
    }
}